package com.xu.chuang.show.ui;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xu.chuang.show.R;
import com.xu.chuang.show.bean.ShowUser;
import com.xu.chuang.show.network.SyncRequest;
import com.xu.chuang.show.service.impl.UserReqImpl;
import com.xu.chuang.show.service.inte.UserReqService;

public class LoginActivity extends AppCompatActivity {

    public static final String ARG_PARAM = "param";

    private UserReqService userReqService = new UserReqImpl();

    private ShowUser showUser;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        SyncRequest.syncReq();

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle(R.string.login);
        setSupportActionBar(toolbar);
        Button signUpButton = (Button) findViewById(R.id.button_welcome_sign_up);
        Button signInButton = (Button) findViewById(R.id.button_welcome_login);
        signUpButton.setOnClickListener(signUpClick);
        signInButton.setOnClickListener(signInClick);
    }

    private Button.OnClickListener signUpClick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(v.getContext(), RegisterActivity.class);
            startActivity(intent);
        }
    };

    private Button.OnClickListener signInClick = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (loginAble()) {
                Intent intent = new Intent(v.getContext(), WingActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(ARG_PARAM, showUser);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }
    };

    private boolean loginAble() {
        AutoCompleteTextView mEmailView = (AutoCompleteTextView) findViewById(R.id.email);
        EditText mPasswordView = (EditText) findViewById(R.id.password);
        String email = mEmailView.getText().toString();
        String password = mPasswordView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid email address.
        if (TextUtils.isEmpty(email)) {
            mEmailView.setError(getString(R.string.error_field_required));
            focusView = mEmailView;
            cancel = true;
        } else if (!isEmailValid(email)) {
            mEmailView.setError(getString(R.string.error_field_invalid));
            focusView = mEmailView;
            cancel = true;
        } else if (TextUtils.isEmpty(password)) {
            mPasswordView.setError(getString(R.string.error_field_required));
            focusView = mPasswordView;
            cancel = true;
        } else if (!isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_field_invalid));
            focusView = mPasswordView;
            cancel = true;
        }

        if (cancel) {
            focusView.requestFocus();
        } else {
            showUser = userReqService.login(email, password);
            if(showUser == null) {
                cancel = true;
                Toast.makeText(this, getString(R.string.error_email_password), Toast.LENGTH_SHORT).show();
            }
        }
        return !cancel;
    }

    private boolean isEmailValid(String emailString) {
        return emailString.contains("@");
    }

    private boolean isPasswordValid(String pwdString) {
        return pwdString.length() >= 6 && pwdString.length() <= 18;
    }
}
